<?php

jimport('joomla.application.component.controller');

class bgController extends JController {
    public function __construct(){
        parent::__construct();
          
        $this->model = $this->getModel();
        $this->aut = new bgAuthorize();
    }
    
    public function display($cachable = false, $urlparams = false) {
        parent::display($cachable, $urlparams);
    }
    public function save(){
        $data = &JRequest::getVar('jform');
        
        // check addnew or edit
        if($this->getModel()->checkExistID($data['COLI_CODE'])){
            $addNew = false;
        }else{
            $addNew = true;
        }
        
        $data['COLI_USER_CREATE'] = $this->aut->getUserID();
        
        /**
         * check data
        **/
        if(empty($data['COLI_COCH_CODE'])){
            //$error = "VUI LONG NHAP MA NGUOI LANH DAO";
            $error = "指導者名（指導員コード）を入力してください。";
            JRequest::setVar('task','error');
            JFactory::getApplication()->enqueueMessage($error, 'error');
        }elseif(empty($data['COLI_OCBA_CODE'])){
            //$error = "VUI LONG NHAP MA TRUNG TAM";
            $error = "研修場所（センターコード）を入力してください。";
            JRequest::setVar('task','error');
            JFactory::getApplication()->enqueueMessage($error, 'error');
        }elseif(empty($data['COLI_TERM'])){
            //$error = "VUI LONG NHAP SO LAN DAO TAO";
            $error = "研修回を入力してください。";
            JRequest::setVar('task','error');
            JFactory::getApplication()->enqueueMessage($error, 'error');
        }else{
            if($this->model->savetemp($data, $addNew)){
                
                $this->setRedirect('index.php?option=com_certification');
               
            }else{
                //JError::raiseError(500, 'Error ! Can\'t save center,club' );
                //$error = "Error! Can't save!";
                $error = "エラー！データが記録されません。";
                JRequest::setVar('task','');
                JFactory::getApplication()->enqueueMessage($error, 'error');
            }
            
        }
        parent::display();
    }
    
    public function remove(){
    	$cid = JRequest::getVar('cid');           
        if($this->model->remove($cid)){
            $this->setRedirect('index.php?option=com_certification');
        }else{
            //JError::raiseError(500, 'Error ! Can\'t delete' );
            $error = "エラー！データが削除されません。";
            JRequest::setVar('task','');
            JFactory::getApplication()->enqueueMessage($error, 'error');
        }
    }
    //search Coch
    public function searchCoch(){
        $search['filter_search'] =  JRequest::getVar('filter_search');
        $this->search_name = $search['filter_search'];
        $result = $this->getModel()->getAllCoch($search);
        
        echo '<div class="the_scroll">
               <table class="adminlist">
            		<thead>
            			<tr>
            				<th class="th10">
            					ID	
                            </th>
                            <th class="th30">
                                氏名
                            </th>
            				<th class="th30">
                                氏名カナ				
                            </th>
                            <th class="th30">
                                性別
                            </th>
            				
            			</tr>
            		</thead>       		
            		<tbody>';
                      
                if($result != null){
                    foreach($result as $k=>$row){
         echo ' <tr class="row'.($k%2).'">
        			<td class="center">
                        <a href="javascript:void(0,0);" class="link_coch_code" onclick="document.id('."'coch_code_temp'".').value='."'". $row->COCH_CODE."'".'">'.$row->COCH_CODE.'</a>
                    </td>
        			<td class="text-algin-left"><a href="javascript:void(0,0);" class="link_coch_code" onclick="document.id('."'coch_code_temp'".').value='."'". $row->COCH_CODE."'".'">'.$row->COCH_NAME.'</a></td>
    			    <td class="text-algin-left">'.$row->COCH_KANA.'</td>';
                if($row->COCH_SEX == 1){
                    echo '<td class="center">男性</td>';
                }else{ 
                    echo '<td class="center">女性</td>';
                }                        
         echo '  </tr>';
                
                    }
                }else{
        echo '      <tr>
                        <td class="center" colspan="4">データは見つかりませんでした。</td>
                    </tr>';
              
                }
             
       	echo '</tbody>
               
            	</table>
                <input type="hidden" name="coch_code_temp" id="coch_code_temp" value="" />
             </div>';
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    //Clear Search Coch
    public function clearSearchCoch(){
        $result = $this->getModel()->getAllCoch();
        
        echo '<div class="the_scroll">
               <table class="adminlist">
            		<thead>
            			<tr>
            				<th class="th10">
            					ID	
                            </th>
                            <th class="th30">
                                氏名
                            </th>
            				<th class="th30">
                                氏名カナ				
                            </th>
                            <th class="th30">
                                性別
                            </th>
            				
            			</tr>
            		</thead>       		
            		<tbody>';
                      
                if($result != null){
                    foreach($result as $k=>$row){
         echo ' <tr class="row'.($k%2).'">
        			<td class="center">
                        <a href="javascript:void(0,0);" class="link_coch_code" onclick="document.id('."'coch_code_temp'".').value='."'". $row->COCH_CODE."'".'">'.$row->COCH_CODE.'</a>
                    </td>
        			<td class="text-algin-left"><a href="javascript:void(0,0);" class="link_coch_code" onclick="document.id('."'coch_code_temp'".').value='."'". $row->COCH_CODE."'".'">'.$row->COCH_NAME.'</a></td>
    			    <td class="text-algin-left">'.$row->COCH_KANA.'</td>';
                if($row->COCH_SEX == 1){
                    echo '<td class="center">男性</td>';
                }else{ 
                    echo '<td class="center">女性</td>';
                }                        
         echo '  </tr>';
                
                    }
                }else{
        echo '      <tr>
                        <td class="center" colspan="4">データは見つかりませんでした。</td>
                    </tr>';
              
                }
             
       	echo '</tbody>
               
            	</table>
                <input type="hidden" name="coch_code_temp" id="coch_code_temp" value="" />
             </div>';
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    //search Ocba
    public function searchOcba(){
        $search['filter_search'] =  JRequest::getVar('filter_search');
        $this->search_name = $search['filter_search'];
        $result = $this->getModel()->getAllOcba($search);
        
        echo '<div class="the_scroll">
               <table class="adminlist">
            		<thead>
            			<tr>
            				<th class="th20">
            					ID
                            </th>
                            <th class="th40">
                                名称
                            </th>
            				<th class="th40">
                                カナ			
                            </th>        				
            			</tr>
            		</thead>      		
            		<tbody>';
                      
                if($result != null){
                    foreach($result as $k=>$row){
         echo ' <tr class="row'.($k%2).'">
        			<td class="center">
                        <a href="javascript:void(0,0);" class="link_ocba_code" onclick="document.id('."'ocba_code_temp'".').value='."'". $row->OCBA_CODE."'".'">'.$row->OCBA_CODE.'</a>
                    </td>
        			<td class="text-algin-left"><a href="javascript:void(0,0);" class="link_ocba_code" onclick="document.id('."'ocba_code_temp'".').value='."'". $row->OCBA_CODE."'".'">'.$row->OCBA_NAME.'</a></td>
    			    <td class="text-algin-left">'.$row->OCBA_KANA.'</td>
                </tr>';
                
                    }
                }else{
        echo '      <tr>
                        <td class="center" colspan="3">データは見つかりませんでした。</td>
                    </tr>';
              
                }
             
       	echo '</tbody>
               
            	</table>
                <input type="hidden" name="ocba_code_temp" id="ocba_code_temp" value="" />
             </div>';
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    //Clear Search Ocba
    public function clearSearchOcba(){
        $result = $this->getModel()->getAllOcba();
        
        echo '<div class="the_scroll">
               <table class="adminlist">
            		<thead>
            			<tr>
            				<th class="th20">
            					ID
                            </th>
                            <th class="th40">
                                名称
                            </th>
            				<th class="th40">
                                カナ			
                            </th>        				
            			</tr>
            		</thead>      		
            		<tbody>';
                      
                if($result != null){
                    foreach($result as $k=>$row){
         echo ' <tr class="row'.($k%2).'">
        			<td class="center">
                        <a href="javascript:void(0,0);" class="link_ocba_code" onclick="document.id('."'ocba_code_temp'".').value='."'". $row->OCBA_CODE."'".'">'.$row->OCBA_CODE.'</a>
                    </td>
        			<td class="text-algin-left"><a href="javascript:void(0,0);" class="link_ocba_code" onclick="document.id('."'ocba_code_temp'".').value='."'". $row->OCBA_CODE."'".'">'.$row->OCBA_NAME.'</a></td>
    			    <td class="text-algin-left">'.$row->OCBA_KANA.'</td>
                </tr>';
                
                    }
                }else{
        echo '      <tr>
                        <td class="center" colspan="3">データは見つかりませんでした。</td>
                    </tr>';
              
                }
             
       	echo '</tbody>
               
            	</table>
                <input type="hidden" name="ocba_code_temp" id="ocba_code_temp" value="" />
             </div>';
        
        $app = &JFactory::getApplication();
        $app->close();
    }

}